package com.tao.utilslib.log;

/*
 * 文件名:     LogUtil
 * 创建者:
 * 创建时间:   2016/10/22 20:21
 * 描述:       输出Log的工具类
 */

import android.os.Environment;
import android.util.Log;

import com.google.gson.Gson;
import com.tao.utilslib.log.logsave.LogSaveManager;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LogUtil {
    //Log开关
    private static boolean isOpen = true;
    private static boolean isSave = false;
    public static String LogPath = Environment.getExternalStorageDirectory() + "/log/";
    //    public static String LogPath = "/log/";
    private static ExecutorService executorService;
    private static final String tag = "[myLog]";
    private static int buffLenth = 1024;
    private static int logFileSize = 10 * 1024 * 1024;

    static {
        executorService = Executors.newSingleThreadExecutor();
    }

    public static String getDateTime() {
        return new SimpleDateFormat("yyyy_MM").format(new Date(System.currentTimeMillis())) + "/";
    }

    public static String getLogPath() {
        return LogPath + getDateTime();
    }

    /**
     * 设置是否输出Log
     *
     * @param b
     */


    public static void outPutLog(boolean b) {
        isOpen = b;
    }

    //**********************Debug***********//
    public static void d(String tag, String msg) {
        if (isOpen) Log.d(tag, msg);
    }

    public static void d(String tag, int msg) {
        if (isOpen) Log.d(tag, String.valueOf(msg));
    }

    public static void d(String tag, float msg) {
        if (isOpen) Log.d(tag, String.valueOf(msg));
    }

    public static void d(String tag, long msg) {
        if (isOpen) Log.d(tag, String.valueOf(msg));
    }

    public static void d(String tag, double msg) {
        if (isOpen) Log.d(tag, String.valueOf(msg));
    }

    public static void d(String tag, boolean msg) {
        if (isOpen) Log.d(tag, String.valueOf(msg));
    }

    //**********************Info***********//
    public static void i(String tag, String msg) {
        if (isOpen) Log.i(tag, msg);
    }

    public static void i(String tag, int msg) {
        if (isOpen) Log.i(tag, String.valueOf(msg));
    }

    public static void i(String tag, float msg) {
        if (isOpen) Log.i(tag, String.valueOf(msg));
    }


    public static void i(String tag, long msg) {
        if (isOpen) Log.i(tag, String.valueOf(msg));
    }

    public static void i(String tag, double msg) {
        if (isOpen) Log.i(tag, String.valueOf(msg));
    }

    public static void i(String tag, boolean msg) {
        if (isOpen) Log.i(tag, String.valueOf(msg));
    }


    public static void e(Object... msgs) {
        if (null == msgs)
            return;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < msgs.length; i++) {
            Object msg = msgs[i];
            if (msg instanceof String || msg instanceof Integer || msg instanceof Boolean || msg instanceof Long || msg instanceof Double || msg instanceof Float) {
                sb.append(String.valueOf(msg));
            } else {
                try {
                    sb.append(new Gson().toJson(msg));
                } catch (Exception e) {
                    e.printStackTrace();
                    sb.append(msg.toString());
                }
            }
        }
        e(tag, sb.toString());
    }

    /**
     * 输出日志
     *
     * @param
     * @param msg
     */
    public static void e(String tag, String msg) {
        if (tag == null)
            return;
        if (isOpen) {
            outPuLog(tag, msg);
            if (isSave) {
                esecuteSacve(getLogPath() + tag + "/Log_E.txt", msg);
            }
        }
    }

    private static void outPuLog(String tag, String msg) {

        int length = msg.length();
        if (length <= buffLenth) {
            Log.e(tag, msg);
        } else {
            int i1 = length / buffLenth;
            for (int i = 0; i <= i1; i++) {
                String substring;
                if (i == i1) {
                    substring = msg.substring(i * buffLenth, i * buffLenth + length % buffLenth);
                } else {
                    substring = msg.substring(i * buffLenth, i * buffLenth + buffLenth);
                }
                outPuLog(tag, substring);
            }

        }


    }

    /**
     * 日志输出按照全局设置，单独指定日志输出到文件
     *
     * @param tag
     * @param msg
     * @param isSave
     */
    public static void e(String tag, String msg, boolean isSave) {
        if (isOpen) outPuLog(tag, msg);
        if (isSave)
            esecuteSacve(getLogPath() + tag + "/Log_E.txt", msg);
    }

    public static void e(String tag, int msg) {
        if (isOpen) Log.e(tag, String.valueOf(msg));
    }

    public static void e(String tag, float msg) {
        if (isOpen) Log.e(tag, String.valueOf(msg));
    }

    public static void e(String tag, Long msg) {
        if (isOpen) Log.e(tag, String.valueOf(msg));
    }

    public static void e(String tag, double msg) {
        if (isOpen) Log.e(tag, String.valueOf(msg));
    }

    public static void e(String tag, boolean msg) {
        if (isOpen) Log.e(tag, String.valueOf(msg));
    }


    public static void saveLogToFile(String path, String str) {

        try {
            createFile(path);
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(new Date(System.currentTimeMillis()));
            str = time + "\n" + str + "\n  ================================== \n\n ";
            LogSaveManager.instance().logServer(path, logFileSize).log(str);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void esecuteSacve(String path, String str) {
 /*       if (Obj.notNULL(str))
            return;*/
        LogUtil.e(str);
//        executorService.submit(new SaveLogTask(path, str));
    }


    public static void mkdirs(String paramString) {
        File localFile = new File(paramString);
        if (!localFile.exists())
            localFile.mkdirs();
    }

    public static synchronized File createFile(String path) throws Exception {
        File file = new File(path);
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        } else if (file.isDirectory()) {
            file.delete();
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        return file;
    }


    private static class SaveLogTask implements Runnable {
        String path;
        String str;

        public SaveLogTask(String path, String str) {
            this.path = path;
            this.str = str;
        }

        @Override
        public void run() {
//            Log.e(tag, "log_path:" + path);
            saveLogToFile(path, str);
        }
    }
}
